package com.storage.mongo;

import java.net.UnknownHostException;
import java.util.Date;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class Test2 {

	
	public  void testdb() throws UnknownHostException, MongoException
	{
		
	Mongo m = new Mongo( "localhost" , 27017 );
	DB db = m.getDB("mydb");
	
	//retrieves a collection or creates one if one does not exist
	DBCollection coll = db.getCollection("testCollection");
	
	BasicDBObject topic = new BasicDBObject();
	BasicDBObject subject = new BasicDBObject();
	
	//category news
	
	subject.put("indiannews", "IPL Starts");
	topic.put("News",subject);
	topic.put("username","prk");
	topic.put("date",new Date());
	coll.insert(topic);
	
	topic.clear();
	subject.clear();
	

	subject.put("indiannews", "pune wariors won");
	topic.put("News",subject);
	topic.put("username","babu");
	topic.put("date",new Date());
	coll.insert(topic);
	
	topic.clear();
	subject.clear();
	

	subject.put("indiannews", "petrol prices hike");
	topic.put("News",subject);
	topic.put("username","mike");
	topic.put("date",new Date());
	coll.insert(topic);
	
	
	topic.clear();
	subject.clear();
	

	subject.put("Chinesenews", "china emerges 1 in aisan games");
	topic.put("News",subject);
	topic.put("username","mike");
	topic.put("date",new Date());
	coll.insert(topic);
	
	
	topic.clear();
	subject.clear();
	

	subject.put("Chinesenews", "bomb scare in beijing");
	topic.put("News",subject);
	topic.put("username","mike");
	topic.put("date",new Date());
	coll.insert(topic);
	
	//to print all documents in collection 
    DBCursor cursor = coll.find();
	while(cursor.hasNext()) {
	    System.out.println(cursor.next());
	}
	
    
	//to remove all documents
    DBCursor cursor1 = coll.find();
	while (cursor1.hasNext()) {
		coll.remove(cursor1.next());

	}
}

}

